package cl.clientews.soap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import cl.clienterest.conexion.ConstantesConexion;


public class PersonasSOAWS {

	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/*
	 * 
	 */
	public void conexionWS(){
		NAMESPACE = ConstantesConexion.soap_namespace; 
		URL = ConstantesConexion.soap_wsdl_persona;
	}

	/**
	 * 
	 * @param rut
	 * @param verificador
	 * @param nombre
	 * @param sexo
	 * @return
	 */
	public String addPersona(String rut, String verificador, String nombre, 
			String sexo){
		
		//int i_sexo = Integer.parseInt(sexo);

		conexionWS();

		METHOD_NAME = "add";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo addrut = new PropertyInfo();
		PropertyInfo addverificador = new PropertyInfo();
		PropertyInfo addnombre = new PropertyInfo();
		PropertyInfo addsexo = new PropertyInfo();
		
		addrut.setName("rut");
		addrut.setValue(rut);
		addrut.setType(String.class);

		addverificador.setName("verificador");
		addverificador.setValue(verificador);
		addverificador.setType(String.class);

		addnombre.setName("nombre");
		addnombre.setValue(nombre);
		addnombre.setType(String.class);
		
		addsexo.setName("sexo");
		addsexo.setValue(sexo);
		addsexo.setType(String.class);
		
		request.addProperty(addrut);
		request.addProperty(addverificador);
		request.addProperty(addnombre);
		request.addProperty(addsexo);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{ //realiza la llamada
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}
	

	/**
	 * 
	 * @param rut
	 * @return
	 */
	public String deletePersona(String rut){

		conexionWS();

		METHOD_NAME = "delete";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo delete = new PropertyInfo();

		delete.setName("rut");
		delete.setValue(rut);
		delete.setType(String.class);

		request.addProperty(delete);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}

	/**
	 * 
	 * @param nombre_o_rut
	 * @param campo
	 * @param new_nom
	 * @return
	 */
	public String updatePersona(String nombre_o_rut, String campo, String new_nom){
		int i_campo = Integer.parseInt(campo);
		conexionWS();

		METHOD_NAME = "update";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo update = new PropertyInfo();
		PropertyInfo updatecampo = new PropertyInfo();
		PropertyInfo updatenew = new PropertyInfo();

		update.setName("nombre_o_rut");
		update.setValue(nombre_o_rut);
		update.setType(String.class);

		updatecampo.setName("campo");
		updatecampo.setValue(i_campo);
		updatecampo.setType(int.class);

		updatenew.setName("new_nom");
		updatenew.setValue(new_nom);
		updatenew.setType(String.class);


		request.addProperty(update);
		request.addProperty(updatecampo);
		request.addProperty(updatenew);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;

	}

	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findPersona(String buscar){

		conexionWS();

		METHOD_NAME = "find";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo find = new PropertyInfo();

		find.setName("buscar");
		find.setValue(buscar);
		find.setType(String.class);

		request.addProperty(find);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}
	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findAllPersona(String buscar){

		conexionWS();

		METHOD_NAME = "findall";


		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findall = new PropertyInfo();

		findall.setName("buscar");
		findall.setValue(buscar);
		findall.setType(String.class);

		request.addProperty(findall);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}

}
